import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import axios from "axios";

const app = createApp(App)
// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
console.log("main.js 正在执行");
// axios.defaults.headers.common['token']=localStorage.getItem('token')
// 在axios请求中添加token到headers
axios.interceptors.request.use(config => {
  console.log("axios拦截器正在执行");
  // 从本地存储获取token，并加入到请求headers中值
  const token = localStorage.getItem('token');
  console.log("从本地存储获取token: " + token);
  if (token !== null) {
    config.headers.token = token;
    console.log("token 已添加到请求头");
  } else {
    console.log("token 不存在");
  }
  console.log("请求头: ", config.headers);
  return config
}, err => {
  console.log("axios拦截器错误处理");
  return Promise.reject(err)
});

app.use(ElementPlus)
app.use(createPinia())
app.use(router)

app.mount('#app')
console.log("app 已挂载");
